﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using Shanda.DevFx.Core;
    using Shanda.DevFx.Security;
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class LoginPage : Page
    {
        protected Button btnLogin;
        protected Label lblMessage;
        protected Label lblReceiveMsg;
        protected RequiredFieldValidator rfvPassword;
        protected RequiredFieldValidator rfvUserName;
        protected TextBox txtDynCode;
        protected TextBox txtPassword;
        protected TextBox txtUserName;

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.DoAuthenticate();
        }

        protected virtual void DoAuthenticate()
        {
            if (!base.get_IsValid())
            {
                this.lblMessage.set_Text("请输入用户名和密码");
            }
            else
            {
                Authentication authentication = base.get_User() as Authentication;
                if (authentication == null)
                {
                    this.lblMessage.set_Text("配置错误");
                }
                else
                {
                    IAOPResult result = authentication.Authenticate(this.txtUserName.get_Text(), this.txtPassword.get_Text(), new object[] { this.txtDynCode.get_Text(), base.get_Request().get_UserHostAddress() });
                    if (!result.IsSuccess)
                    {
                        this.lblMessage.set_Text("登录失败：" + result.ResultDescription);
                    }
                    else
                    {
                        this.RedirectToUrl(authentication);
                    }
                }
            }
        }

        protected virtual void InitData()
        {
            if (!base.get_IsPostBack())
            {
                this.lblReceiveMsg.set_Text(base.get_Request().get_Params()["ErrorMsg"]);
                if (base.get_User().get_Identity().get_IsAuthenticated())
                {
                    this.lblMessage.set_Text(string.Format("您已经作为 <u>{0}</u> 登录了，是否要换个帐号登录？", base.get_User().get_Identity().get_Name()));
                }
            }
        }

        private void InitializeComponent()
        {
            this.btnLogin.add_Click(new EventHandler(this.btnLogin_Click));
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.InitData();
        }

        protected virtual void RedirectToUrl(Authentication authentication)
        {
            HttpCookie authCookie = authentication.GetAuthCookie();
            base.get_Response().AppendCookie(authCookie);
            base.get_Response().Redirect(authentication.GetRedirectUrl(base.get_Request()));
        }
    }
}

